<?php

class IndexAction extends Action
{

    public function verify($reportid)
    {
        if (empty($reportid))
        {
            //$this->redirect('__ROOT__/index.php/Login/login/', 5, '没有该报表，页面跳转中...');
            $this->error('没有该报表');
        }
    }

    public function index()
    {
        $reportid = $_GET['reportid'];

        $this->verify($reportid);
        $Base = A('Base');

        $alliteminfo = $Base->getAllItemInfo($reportid);

        $iteminfo = $Base->getItemInfo($reportid);   //包含首页所有体检项目变量的数组
        $reportinfo = $Base->reportInfo($reportid);   //获取个人信息
//        var_dump($reportinfo);exit();

        $username = $reportinfo['username'];
        if (empty($reportinfo['birthday']))
        {
            $userage = null;
        }
        else
        {
            $userage = date('Y') - substr($reportinfo['birthday'], 0, 4);
        }
        $sex = $reportinfo['gender'];
        $examtime = date("Y/m/d", strtotime($reportinfo['examtime']));

        //获取用户的身高和体重
        foreach ($iteminfo as $key => $val)
        {
            if (substr($val['itemname'], 0, 6) == "体重" && substr($val['itemname'], 0, 12) !== "体重指数")
            {
                $weight = $val['result'];
            }
            if (substr($val['itemname'], 0, 6) == "身高")
            {
                $height = $val['result'];
            }
        }
        //ptr($Base->getIssueItemInfo($iteminfo));die();
        $issueItemCount = count($Base->getIssueItemInfo($iteminfo));
        $passItemCount = count($iteminfo) - $issueItemCount;
        $pass = round(($passItemCount) / ($issueItemCount + $passItemCount) * 100);

        $zitem = $Base->getResult($alliteminfo, '总检');
        if (!$zitem)
        {
            $zitem = $Base->getResult($alliteminfo, '总汇');
        }
        $zitems = $Base->summary($zitem);

        $organ = $Base->totalOrganStatus($iteminfo);
        $issueorgancount = $Base->issueOrgancount($organ);
        $itemCount = $Base->organItemCount($organ);

        $array['name'] = $username;
        $array['age'] = $userage;
        $array['sex'] = $sex;
        $array['height'] = $height;
        $array['weight'] = $weight;
        $array['issueitemcount'] = $issueItemCount;
        $array['passitemcount'] = $passItemCount;
        $array['pass'] = $pass;

        //获取用户的bmi，不同的bmi加载不同的人物图片
        //$array['bmi'] = round($weight / (($height * 0.01) * ($height * 0.01)), 1);
        foreach ($iteminfo as $key1 => $value1)
        {
            if (substr($value1['itemname'], 0, 12) == '体重指数')
            {
                $array['bmi'] = $value1['result'];
            }
        }

        $array['bmip'] = ($array['bmi'] - 7.6) * 5.07;
        $array['bmipic'] = round(($array['bmi'] - 7.6) / 1.68);
        $array['examtime'] = $examtime;

        $this->assign('user', $array);
        $this->assign('items', $zitems);
        $this->assign('issuecount', $issueorgancount);
        $this->assign('itemcount', $itemCount);
        $this->assign('reportid', $reportid);

        $this->display();
    }

    public function list1()
    {
        $reportid = $_GET['reportid'];

        $Base = A('Base');

        $reportinfo = $Base->reportInfo($reportid);
        $allPrograms = $Base->programInfo($reportid);
        $allRealItems = $Base->getItemInfo($reportid);
        $allItems = $Base->getAllItemInfo($reportid);

        $username = $reportinfo['username'];
        $userage = date('Y') - substr($reportinfo['birthday'], 0, 4);
        $sex = $reportinfo['gender'];

        $all = $Base->rawReport($allItems, $allPrograms);
        $name = $all[0];
        $piclist = $all[1];
        $item = $all[2];

        foreach ($item as $key => $value)
        {
            foreach ($value as $key1 => $value1)
            {
                if ($value1['itemname'] == '总检')
                {
                    $zj = $value1['result'];
                    //单一龙
                    unset($item[$key]);
                }
            }
        }

        $array['name'] = $username;
        $array['age'] = $userage;
        $array['sex'] = $sex;

        $this->assign('user', $array);
        $this->assign('piclist', $piclist);
        $this->assign('name', $name);
        $this->assign('items', $item); //back  $all
        $this->assign('zj', $zj);

        $this->assign('reportid', $reportid);
        $this->assign('organid', $organid);

        $this->display();
    }

    public function test()
    {
//        $reportid = $_GET['id'];
//
//        $Base = A('Base');
//        
        //$lsit = $Base->reportInfo($reportid);
//        $allPrograms = $Base->programInfo($reportid);
//        $allRealItems = $Base->getItemInfo($reportid);
//        $allItems = $Base->getAllItemInfo($reportid);
        //$a = $Base->getIssueItemInfo($lsit);
        //array_splice($a,2,1);
        //$temp = $lsit[0]['身高'];
        //$value = array_search('总检', $lsit);
//        dump($lsit);
        /*
          $lsit1 = $Base->getResult($lsit, '总检');
          dump($lsit1);
          $list2 = $Base->summary($lsit1);

          dump($list2); */

//        dump($allItems);
//        $a = $Base->rawReport($allItems, $allPrograms);
//        dump($a);
        //dump($Base->singleOrganStatus(10, $allRealItems));
        //dump($Base->totalOrganStatus($allRealItems));
//        dump($Base->rawReport($allItems, $allPrograms));
    }

    public function more()
    {
        $reportid = $_GET['reportid'];
        $organid = $_GET['organid'];
        $itemid = $_GET['itemid'] - 1;
        $issue = $_GET['issue'];

        $Base = A('Base');

        $reportinfo = $Base->reportInfo($reportid);
        $iteminfo = $Base->getItemInfo($reportid);
        $singleOrgan = $Base->singleOrganStatus($organid, $iteminfo);

        $username = $reportinfo['username'];
        if (empty($reportinfo['birthday']))
        {
            $userage = null;
        }
        else
        {
            $userage = date('Y') - substr($reportinfo['birthday'], 0, 4);
        }
        $sex = $reportinfo['gender'];

        $array['name'] = $username;
        $array['age'] = $userage;
        $array['sex'] = $sex;
//        var_dump($array);exit();

        $this->assign('user', $array);
        $this->assign('vo', $singleOrgan[$issue][$itemid]);

        $this->display();
    }

    public function organ()
    {
        $reportid = $_GET['reportid'];
        $organid = $_GET['organid'];

        $Base = A('Base');

        $reportinfo = $Base->reportInfo($reportid);
        $iteminfo = $Base->getItemInfo($reportid);

        $username = $reportinfo['username'];
        if (empty($reportinfo['birthday']))
        {
            $userage = null;
        }
        else
        {
            $userage = date('Y') - substr($reportinfo['birthday'], 0, 4);
        }
        $sex = $reportinfo['gender'];

        $singleorgan = $Base->singleOrganStatus($organid, $iteminfo);
        $item['issuecount'] = count($singleorgan[0]);
        $item['itemcount'] = count($singleorgan[1]) + count($singleorgan[0]);

        $array['name'] = $username;
        $array['age'] = $userage;
        $array['sex'] = $sex;

        $this->assign('user', $array);
        $this->assign('item', $item);
        $this->assign('issueorgan', $singleorgan[0]);
        $this->assign('organ', $singleorgan[1]);
        //dump($singleorgan);

        $this->assign('reportid', $reportid);
        $this->assign('organid', $organid);

        $this->display();
    }

    public function lists()
    {
        $reportid = $_GET['reportid'];

        $Base = A('Base');

        $reportinfo = $Base->reportInfo($reportid);
        $allPrograms = $Base->programInfo($reportid);
        $allRealItems = $Base->getItemInfo($reportid);
        $allItems = $Base->getAllItemInfo($reportid);

        $username = $reportinfo['username'];
        if (empty($reportinfo['birthday']))
        {
            $userage = null;
        }
        else
        {
            $userage = date('Y') - substr($reportinfo['birthday'], 0, 4);
        }

        $sex = $reportinfo['gender'];

        $all = $Base->rawReport($allItems, $allPrograms);
        $name = $all[0];
//        var_dump($name);exit();
        $piclist = $all[1];
        $item = $all[2];
//         var_dump($item);exit();

        foreach ($item as $key => $value)
        {
            foreach ($value as $key1 => $value1)
            {
                if ($value1['itemname'] == '总检' || $value1['itemname'] == '总汇')
                {
                    $zj = $value1['result'];

                    unset($item[$key]);
                }

                if (!empty($value1['normal']) && !empty($value1['result']))
                {

                    if ($value1['result'] == "(-)")
                    {
                        $value1['rang'] = "50";
                        $value[$key1] = $value1;
                    }
                    else
                    {
//                    var_dump($value1);die();
                        $ran = explode('-', $value1['normal']);
                        $low = floatval($ran[0]);
                        $high = floatval($ran[1]);
                        $result = floatval($value1['result']);
                        $per = ($result - $low) / ($high - $low) * 100;
  
                        $per = number_format($per, 0);
                        $value1['rang'] = $per;
                        $value[$key1] = $value1;
                    }
                }
            }
            $item[$key] = $value;
        }

//        die();
        //var_dump($item);
//        foreach ($item as $one)
//        {
//            foreach ($one as $oneone)
//            {
//                if (!empty($oneone['normal']) && !empty($oneone['result']))
//                {
//
//                    $ran = explode('-', $oneone['normal']);
//                    $low = (int) $ran[0];
//                    $high = (int) $ran[1];
//                    $subdown = $high - $low;
//                    $subup = $oneone['result'];
//                    $per = ($subup / $subdown) * 100;
//                    $per = number_format($per, 0);
//                    $oneone['rang'] = $per;
//                }
//            }
//        }

        // var_dump($zj);exit();
        $array['name'] = $username;
        $array['age'] = $userage;
        $array['sex'] = $sex;

        $this->assign('user', $array);
        $this->assign('piclist', $piclist);
        $this->assign('name', $name);
        $this->assign('items', $item); //back  $all
        $this->assign('zj', $zj);

        //var_dump($item);die();

        $this->assign('reportid', $reportid);
        $this->assign('organid', $organid);

        $this->display();
    }

    public function bmiinfo()
    {
        $reportid = $_GET['reportid'];

        $Base = A('Base');

        $reportinfo = $Base->reportInfo($reportid);
        $iteminfo = $Base->getItemInfo($reportid);

        $username = $reportinfo['username'];

        if (empty($reportinfo['birthday']))
        {
            $userage = null;
        }
        else
        {
            $userage = date('Y') - substr($reportinfo['birthday'], 0, 4);
        }

        $sex = $reportinfo['gender'];


//        $height = $iteminfo[0]['result'];
//        $weight = $iteminfo[1]['result'];
        //获取用户的身高和体重
        foreach ($iteminfo as $key => $val)
        {
            if (substr($val['itemname'], 0, 6) == "体重" && substr($val['itemname'], 0, 12) !== "体重指数")
            {
                $weight = $val['result'];
            }
            if (substr($val['itemname'], 0, 6) == "身高")
            {
                $height = $val['result'];
            }
        }
        // var_dump($height);exit();
//        $array['bmi'] = round($weight / (($height * 0.01) * ($height * 0.01)), 1);
        //获取用户的bmi，不同的bmi加载不同的人物图片
        //$array['bmi'] = round($weight / (($height * 0.01) * ($height * 0.01)), 1);
        foreach ($iteminfo as $key1 => $value1)
        {
            if (substr($value1['itemname'], 0, 12) == '体重指数')
            {
                $array['bmi'] = $value1['result'];
            }
        }
//        var_dump($array['bmi']);exit();

        $array['name'] = $username;
        $array['age'] = $userage;
        $array['sex'] = $sex;
        // $array['bmi'] = round($weight / (($height * 0.01) * ($height * 0.01)), 1);
        $array['bmip'] = ($array['bmi'] - 7.2) * 5.07;
        $array['bmipic'] = round(($array['bmi'] - 7.6) / 1.68);

        // var_dump($array['bmipic']);exit();
        //如果用户的bmi不正常，则默认加载一张图片
        if ($array['bmipic'] < 0)
        {
            $array['bmipic'] = 13;
        }
        // var_dump($array['bmipic']);exit();

        $this->assign('user', $array);

        $this->display();
    }

    public function unusual()
    {
        $reportid = $_GET['reportid'];

        $Base = A('Base');

        $reportinfo = $Base->reportInfo($reportid);
        $iteminfo = $Base->getItemInfo($reportid);
        $allPrograms = $Base->programInfo($reportid);
        $allItems = $Base->getAllItemInfo($reportid);

        $all = $Base->rawReport($allItems, $allPrograms);
        $name = $all[0];
        $item = $all[2];

        foreach ($item as $key => $value)
        {
            foreach ($value as $key1 => $value1)
            {
                if ($value1['itemname'] == '总检')
                {
                    $zj = $value1['result'];
                    //单一龙
                    unset($item[$key]);
                }
                if ($value1['itemname'] == '总汇')
                {
                    $zj = $value1['result'];
                    //单一龙
                    unset($item[$key]);
                }
            }
        }

        $username = $reportinfo['username'];
        if ($reportinfo['birthday'])
        {
            $userage = date('Y') - substr($reportinfo['birthday'], 0, 4);
        }
        else
        {
            $userage = "";
        }

        $sex = $reportinfo['gender'];

        $array['name'] = $username;
        $array['age'] = $userage;
        $array['sex'] = $sex;

//         var_dump($item);exit();
        $this->assign('user', $array);
        $this->assign('items', $item);
        $this->assign('name', $name);

        $this->assign('reportid', $reportid);

        $this->display();
    }

    public function usual()
    {
        $reportid = $_GET['reportid'];

        $Base = A('Base');

        $reportinfo = $Base->reportInfo($reportid);
        $iteminfo = $Base->getItemInfo($reportid);
        $allPrograms = $Base->programInfo($reportid);
        $allItems = $Base->getAllItemInfo($reportid);

        $all = $Base->rawReport($allItems, $allPrograms);
        $name = $all[0];
        $item = $all[2];

        foreach ($item as $key => $value)
        {
            foreach ($value as $key1 => $value1)
            {
                if ($value1['itemname'] == '总检')
                {
                    $zj = $value1['result'];
                    //单一龙
                    unset($item[$key]);
                }
                if (!empty($value1['normal']) && !empty($value1['result']))
                {

                    if ($value1['result'] == "(-)")
                    {
                        $value1['rang'] = "50";
                        $value[$key1] = $value1;
                    }
                    else
                    {
//                    var_dump($value1);die();
                        $ran = explode('-', $value1['normal']);
                        $low = floatval($ran[0]);
                        $high = floatval($ran[1]);
                        $result = floatval($value1['result']);
                        $per = ($result - $low) / ($high - $low) * 100;

                        $per = number_format($per, 0);
                        $value1['rang'] = $per;
                        $value[$key1] = $value1;
                    }
                }
            }
             $item[$key] = $value;
        }

//        die();
        $username = $reportinfo['username'];
        if ($reportinfo['birthday'])
        {
            $userage = date('Y') - substr($reportinfo['birthday'], 0, 4);
        }
        else
        {
            $userage = "";
        }
//        $userage = date('Y') - substr($reportinfo['birthday'], 0, 4);
        $sex = $reportinfo['gender'];

        $array['name'] = $username;
        $array['age'] = $userage;
        $array['sex'] = $sex;

        $this->assign('user', $array);
        $this->assign('items', $item);
        $this->assign('name', $name);

        $this->assign('reportid', $reportid);

        $this->display();
    }

    function array_remove_value(&$arr, $var)
    {
        foreach ($arr as $key => $value)
        {
            if (is_array($value))
            {
                array_remove_value($arr[$key], $var);
            }
            else
            {
                $value = trim($value);
                if ($value == $var)
                {
                    unset($arr[$key]);
                }
                else
                {
                    $arr[$key] = $value;
                }
            }
        }
    }

}
